import java.time.LocalDateTime;
import java.util.*;

public class UserManager {
    private Map<String, User> usersByUsername = new HashMap<>();
    private Map<String, User> usersByPhone = new HashMap<>();
    private Map<String, User> usersById = new HashMap<>();
    private int userIdSeq = 1000;

    public void initDefaultAdmin() {
        if (!usersByUsername.containsKey("admin")) {
            Admin admin = new Admin("A1000", "admin", "ynuinfo#777", LocalDateTime.now(), "10000000000");
            usersByUsername.put(admin.getUsername(), admin);
            usersByPhone.put(admin.getPhone(), admin);
            usersById.put(admin.getUserId(), admin);
        }
    }

    public User login(String username, String password) {
        User user = usersByUsername.get(username);
        if (user != null && user.getPassword().equals(password)) {
            return user;
        }
        return null;
    }

    public User register(String userType, String username, String password, String phone) {
        if (usersByUsername.containsKey(username)) {
            throw new IllegalArgumentException("用户名已存在");
        }
        if (usersByPhone.containsKey(phone)) {
            throw new IllegalArgumentException("手机号已注册");
        }
        String userId = userType.charAt(0) + String.valueOf(userIdSeq++);
        User user;
        switch (userType) {
            case "经理":
                user = new Manager(userId, username, password, LocalDateTime.now(), phone);
                break;
            case "前台":
                user = new FrontDesk(userId, username, password, LocalDateTime.now(), phone);
                break;
            default:
                throw new IllegalArgumentException("仅支持注册经理或前台用户");
        }
        usersByUsername.put(username, user);
        usersByPhone.put(phone, user);
        usersById.put(userId, user);
        return user;
    }

    public boolean deleteUser(String username) {
        User user = usersByUsername.remove(username);
        if (user != null) {
            usersByPhone.remove(user.getPhone());
            usersById.remove(user.getUserId());
            return true;
        }
        return false;
    }

    public User findUserByUsername(String username) {
        return usersByUsername.get(username);
    }

    public User findUserByPhone(String phone) {
        return usersByPhone.get(phone);
    }

    public User findUserById(String userId) {
        return usersById.get(userId);
    }

    public boolean updateUser(String username, String newPassword, String newPhone) {
        User user = usersByUsername.get(username);
        if (user == null) return false;
        if (newPassword != null && !newPassword.isEmpty()) {
            user.setPassword(newPassword);
        }
        if (newPhone != null && !newPhone.isEmpty() && !newPhone.equals(user.getPhone())) {
            if (usersByPhone.containsKey(newPhone)) {
                throw new IllegalArgumentException("新手机号已被注册");
            }
            usersByPhone.remove(user.getPhone());
            user.phone = newPhone;
            usersByPhone.put(newPhone, user);
        }
        return true;
    }

} 